package _022_2_12

func wordsDFS(visitied []int, words []string, word string, maxStr *string) {
	for i := 0; i < len(visitied); i++ {
		if visitied[i] != 1 && len(word) >= len(words[i]) {
			charI := 0
			for ; charI < len(words[i]); charI++ {
				if words[i][charI] != word[charI] {
					break
				}
			}

			if charI == len(words[i]) {
				if len(words[i]) == len(word) {
					if len(word) > len(*maxStr) {
						*maxStr = word
					}
				}
				visitied[i] = 1
				wordsDFS(visitied, words, word[charI:], maxStr)
				visitied[i] = 0
			}
		}
	}
}
func longestWord(words []string) string {
	maxStr := ""
	visited := make([]int, len(words))
	for i := 0; i < len(words); i++ {
		visited[i] = 1
		wordsDFS(visited, words, words[i], &maxStr)
		visited[i] = 0
	}
	return maxStr
}
